package com.rx.school.service.proxy;

import com.rx.core.base.RxBaseData;
import com.rx.core.enm.HANDLER_TYPE;
import com.rx.core.service.StandardService;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxUtil;
import com.rx.school.service.feign.FeignOrgInfoService;
import com.rx.uua.vo.UuaOrgInfo;
import com.rx.uua.vo.UuaUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class OutProxyService {

    @Autowired
    StandardService standardService;

    @Autowired
    FeignOrgInfoService feignOrgInfoService;

    public UuaOrgInfo getDetail(UuaOrgInfo data){

        try {
            UuaOrgInfo uuaOrgInfo = (UuaOrgInfo) RxBaseData.create(UuaOrgInfo.class, HANDLER_TYPE.SINGLE_DETAIL);
            RxUtil.copyProperties(data,uuaOrgInfo);
            uuaOrgInfo.getHandlerController().onlyDefault();

            return (UuaOrgInfo)standardService.detail(uuaOrgInfo);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

        return feignOrgInfoService.getDetail(data);
    }

    public UuaUserInfo getUserDetail(UuaUserInfo data){

        try {
            UuaUserInfo uuaUserInfo = (UuaUserInfo) RxBaseData.create(UuaUserInfo.class, HANDLER_TYPE.SINGLE_DETAIL);
            RxUtil.copyProperties(data,uuaUserInfo);
            uuaUserInfo.getHandlerController().onlyDefault();
            uuaUserInfo.appendSqlFilter(UuaUserInfo.removeField());
            return (UuaUserInfo)standardService.detail(uuaUserInfo);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

        return feignOrgInfoService.getUserDetail(data);
    }

    public List<UuaUserInfo> getUserList(List<UuaUserInfo> uuaUserInfoList){

        if(EmptyChecker.isEmpty(uuaUserInfoList)){
            return Collections.emptyList();
        }
        try {
            UuaUserInfo uuaUserInfo = (UuaUserInfo) RxBaseData.create(UuaUserInfo.class, HANDLER_TYPE.SINGLE_DETAIL);
            Map<String,Object> map = new HashMap<>();
            List<String> bidList = uuaUserInfoList.stream().map(e->e.getBid()).collect(Collectors.toList());
            map.put("inBid",bidList);
            uuaUserInfo.setCondition(map);
            uuaUserInfo.getHandlerController().onlyDefault();
            uuaUserInfo.appendFilter(UuaUserInfo.removeField());
            return (List)standardService.searchList(uuaUserInfo);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

        return feignOrgInfoService.getUserList(uuaUserInfoList);
    }
}
